package org.jimm.app;

import org.jimm.protocol.AccountsManager;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;

public class NotificationMan {

    private static final int ID_NEW_MSG = 0xFF;
    private static final int ID_APP_ICON = 0xFE;

    private static Notification notificationApp = null;
    private static Notification notificationMsg = null;
    private static NotificationManager notificationManager;
    private static AccountsManager accountsManager;


    public static void updateNotifications(Context context) {
        accountsManager = JimmApplication.getInstance().getAccountsManager();
        notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        if (JimmApplication.getInstance().getPreferences().showAppIcon) {
            if (notificationApp == null) {
                notificationApp = new Notification(R.drawable.ic_app, context.getString(R.string.app_name), System.currentTimeMillis());
            }
            PendingIntent contentIntent = PendingIntent.getActivity(context, 0, new Intent(context, org.jimm.app.ContactListActivity.class), 0);
            CharSequence statusText = context.getText(R.string.status_offline);

            if (accountsManager != null) {
                if (accountsManager.isConnecting())
                    statusText = context.getText(R.string.status_connecting);
                else if (accountsManager.isConnected())
                    statusText = context.getText(R.string.status_online);
            }
            notificationApp.setLatestEventInfo(context, context.getText(R.string.app_name), statusText, contentIntent);
            notificationApp.flags |= Notification.FLAG_NO_CLEAR;

            notificationManager.notify(ID_APP_ICON, notificationApp);
        }

        if (notificationMsg == null) {
            notificationMsg = new Notification(R.drawable.ic_message, context.getString(R.string.msg_new), System.currentTimeMillis());
        }
        Intent intent = new Intent(context, ContactListActivity.class);

        int unreadMsgCount = 0;

        if (accountsManager != null) {
            accountsManager.getUnreadMessageCount();
        }

        CharSequence contentTitle = context.getString(R.string.app_name);
        CharSequence contentText = "";

        switch (unreadMsgCount) {

            case 0:
                notificationManager.cancel(ID_NEW_MSG);
                return;

            case 1:
                contentText = context.getString(R.string.msg_one);
                break;

            case 2:
            case 3:
            case 4:
                contentText = context.getString(R.string.msg_several).replace("$n", "" + unreadMsgCount);
                break;

            default:
                contentText = context.getString(R.string.msg_many).replace("$n", "" + unreadMsgCount);
                break;
        }

        PendingIntent contentIntent = PendingIntent.getActivity(context, 0, intent, 0);
        notificationMsg.setLatestEventInfo(context, contentTitle, contentText, contentIntent);

        notificationManager.cancel(ID_NEW_MSG);
        notificationManager.notify(ID_NEW_MSG, notificationMsg); // show notify
    }

    public static void clearNotifications() {
        try {
            notificationManager.cancel(ID_NEW_MSG);
            notificationManager.cancel(ID_APP_ICON);
        } catch (NullPointerException e) {

        }
    }
}
